package org.classg.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.classg.edu.entity.Course;
import org.classg.edu.entity.CourseDescription;
import org.classg.edu.entity.Teacher;
import org.classg.edu.mapper.CourseMapper;
import org.classg.edu.service.*;
import org.classg.edu.vo.CourseInfoVo;
import org.classg.edu.vo.CoursePublishInfoVo;
import org.classg.edu.vo.front.CourseInfoFrontVo;
import org.classg.edu.vo.front.CourseQueryFrontVo;
import org.classg.servicebase.exception.MyException;
import org.classg.vo.CourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private TeacherService teacherService;

    // 添加课程基本信息
    @Override
    public String queryCourseInfo(CourseInfoVo courseInfoVo) {
        // 添加课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        // 设置SubjectParentId

        System.out.println(course);
        int temp1 = baseMapper.insert(course);
        if (temp1 == 0) {
            // 添加失败
            throw new MyException(20001, "课程基本信息添加失败");
        }

        // 添加课程详情信息（与课程1对1）
        CourseDescription courseDescription = new CourseDescription();
        // 获取上面添加了课程基本信息的id
        courseDescription.setId(course.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        boolean temp2 = courseDescriptionService.save(courseDescription);
        if (!temp2) {
            // 添加失败
            throw new MyException(20001, "课程详情信息添加失败");
        }
        return course.getId();
    }

    // 根据课程id查询课程基本信息
    @Override
    public CourseInfoVo queryCourseInfoByCourseId(String courseId) {
        // 根据课程id查询课程
        Course course = baseMapper.selectById(courseId);
        // 根据课程id查询课程简介
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        // 封装
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course, courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    // 根据课程id修改课程基本信息
    @Override
    public void updateCourseInfoByCourseId(CourseInfoVo courseInfoVo) {
        // 更新课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        int temp1 = baseMapper.updateById(course);
        if (temp1 == 0) {
            throw new MyException(20001, "课程基本信息更新失败！");
        }
        // 更新课程详情信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        boolean temp2 = courseDescriptionService.updateById(courseDescription);
        if (!temp2) {
            throw new MyException(20001, "课程详情信息更新失败！");
        }
    }

    // 根据课程id获取课程最终发布数据
    @Override
    public CoursePublishInfoVo queryCoursePublishInfoById(String id) {
        CoursePublishInfoVo coursePublishInfoVo = baseMapper.queryCoursePublishInfoById(id);
        return coursePublishInfoVo;
    }

    // 根据id删除课程
    @Override
    public Boolean deleteCourseById(String id) {
        // 1. 根据课程id删除小节
        videoService.deleteVideoByCourseId(id);
        // 2. 根据课程id删除章节
        chapterService.deleteChapterByCourseId(id);
        // 3. 根据课程id删除课程简介
        courseDescriptionService.removeById(id);
        // 4. 根据课程id删除课程
        int temp = baseMapper.deleteById(id);
        if (temp > 0) {
            return true;
        } else {
            throw new MyException(20001, "根据id删除课程失败！");
        }
    }

    // 根据讲师id查询讲师所讲的课程
    @Override
    public List<Course> queryCourseByTeacherId(String id) {
        return baseMapper.selectList(new LambdaQueryWrapper<Course>().eq(Course::getTeacherId, id));
    }

    // 查询所有课程分页数据包含查询条件
    @Override
    public Map<String, Object> queryAllCoursePage(Page<Course> page, CourseQueryFrontVo courseQueryFrontVo) {
        // new一个查询对象
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        // 判断条件
        if (!StringUtils.isEmpty(courseQueryFrontVo.getSubjectParentId())) {
            wrapper.eq(Course::getSubjectParentId, courseQueryFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQueryFrontVo.getSubjectId())) {
            wrapper.eq(Course::getSubjectId, courseQueryFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQueryFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc(Course::getBuyCount);
        }
        if (!StringUtils.isEmpty(courseQueryFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc(Course::getGmtCreate);
        }
        if (!StringUtils.isEmpty(courseQueryFrontVo.getPriceSort())) {
            wrapper.orderByDesc(Course::getPrice);
        }
        // 查询数据
        baseMapper.selectPage(page, wrapper);
        List<Course> courses = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        // 把分页数据封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("courses", courses);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    // 根据课程id查询课程详情信息
    @Override
    public CourseInfoFrontVo queryCourseInfoVoByCourseId(String id) {
        return baseMapper.queryCourseInfoVoByCourseId(id);
    }

    // 根据课程id查询课程数据
    @Override
    public CourseVo queryCourseById(String id) {
        CourseVo courseVo = null;
        try {
            Course course = baseMapper.selectOne(new LambdaQueryWrapper<Course>().eq(Course::getId, id));
            // 根据讲师的id查询讲师信息
            Teacher teacher = teacherService.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getId, course.getTeacherId()));
            // 根据
            courseVo = new CourseVo();
            BeanUtils.copyProperties(course, courseVo);
            courseVo.setTeacherName(teacher.getName());
        } catch (BeansException e) {
            throw new MyException(20001, "根据课程id查询讲师课程数据失败！");
        }
        return courseVo;
    }
}
